package cn.darkhorse.alarm.service.alarm;

import cn.darkhorse.alarm.pojo.AlarmParameterSettings;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 变量，连续预警
 * @author yuanjun
 * @since 2023/4/7 13:46
 */
@Component
public class ContinuousAlarm extends BaseAlarm implements Alarm{
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void doAlarm(AlarmParameterSettings settings, Map<String,Object> data) {
        String value = (String) data.get(settings.getFieldName());
        if(StringUtils.isBlank(value)) {
            return;
        }

        if (settings.getAlarmType().equals(ALARM_CONTINUOUS_VALUE)) {
            // 获取最大最小值
            String alarmMessage = settings.getAlarmMessage();
            JSONArray jsonArray = JSONArray.parse(alarmMessage);
            for (Object obj : jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                String valueRedis = (String) jsonObject.get("value");

                if (valueRedis.equals(value)){
                    // 值相等需要预警
                    sendAlarmFlag(settings,data);
                }
            }
        }
    }
    
}
